﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MadProps
{
    public sealed class PropId : IHavePropId
    {
        public ClrMember ClrMember { get; private set; }
        public Type DeclaringType { get { return ClrMember.DeclaringType; } }
        public string ClrName { get { return ClrMember.Name; } }

        internal PropId(ClrMember clrMember)
        {
            this.ClrMember = clrMember.ThrowIfNull("member");

            if (!typeof(IProp).IsAssignableFrom(clrMember.Type))
            {
                throw new InvalidOperationException("member does not define an IProperty");
            }
        }

        public override bool Equals(object obj)
        {
            return this == (obj as PropId);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return DeclaringType.GetHashCode() + ClrName.GetHashCode();
            }
        }

        public static bool operator ==(PropId one, PropId two)
        {
            if (object.ReferenceEquals(one, two))
            {
                return true;
            }
            if (null == one || null == two)
            {
                return false;
            }

            return one.DeclaringType.Equals(two.DeclaringType)
                && one.ClrName.Equals(two.ClrName);
        }

        public static bool operator !=(PropId one, PropId two)
        {
            return !(one == two);
        }

        PropId IHavePropId.PropId
        {
            get { return this; }
        }
    }
}
